// pages/userInfo/userInfo.js
Page({

      /**
       * 页面的初始数据
       */
      data: {
            nickName:'',
            addr:'',    // 房屋号码
            number:'',  // 房产证编号
            phone:'',
            cars:'',    // 车辆牌照
            parkingSpace:'',   // 车位编号
            housingId:'',
            communityList:[
            ],
            selectIndex:0
      },
      submit(){
            if(this.data.phone=='' || this.data.addr==''|| this.data.number==''){
                  wx.showToast({
                    title: '带*项为必填',
                    icon:'error'
                  })
            }else{
                  wx.showLoading({
                    title: '提交信息',
                  })
                  
                  // 验证手机号合法性
                  if(!(this.testPhone(this.data.phone))){
                        wx.showToast({
                              title: '手机号格式有误',
                              icon:'error'
                        })
                        return;
                  }

                  // 提交信息 未完
                  let userInfo=this.data;
                  let userId=wx.getStorageSync('userId');
                  userInfo.id=userId;

                  wx.request({
                        url: getApp().globalData.host+'/client/user/submitInfo',
                        method:'POST',
                        data:userInfo,
                        header: {
                              'content-type': 'application/x-www-form-urlencoded'
                        },
                        success:res=>{
                              if(res.data.status.code==200){
                                    wx.setStorageSync('userInfo', res.data.data.userInfo);
                                    wx.navigateBack({
                                      delta: 1,
                                    })
                                    wx.showToast({
                                      title: '保存成功',
                                    })
                              }
                        },
                        complete:()=>{
                              wx.hideLoading();
                        }
                  })
            }
            
      },
      nickNameInput(e){
            this.setData({
                  nickName:e.detail.value
            })
      },
      phoneInput(e){
            this.setData({
                  phone:e.detail.value
            })
      },
      /**检测手机号是否合法 */
      testPhone(phone){
            var myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
            if (!myreg.test(phone)) {
                  return false;
            } else {
                  return true;
            }
      },
      addrInput(e){
            this.setData({
                  addr:e.detail.value
            })
      },
      numberInput(e){
            this.setData({
                  number:e.detail.value
            })
      },
      carsInput(e){
            this.setData({
                  cars:e.detail.value
            })
      },
      parkingSpaceInput(e){
            this.setData({
                  parkingSpace:e.detail.value
            })
      },

      selectCommunity(e){
            this.setData({
                  housingId:this.data.communityList[e.detail.value].id
            })
      },
      closeSelect(){
            
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
            wx.showLoading({
              title: '加载个人信息',
            })
            let userInfo=wx.getStorageSync('userInfo');
            this.setData({
                  nickName:userInfo.nickName
            })
            let userId=wx.getStorageSync('userId');
            wx.request({
                  url: getApp().globalData.host+'/client/user/info?id='+userId,
                  method:'GET',
                  success:res=>{
                        this.setData({
                              communityList:res.data.data.communityList,
                              housingId:res.data.data.userInfo.housingId||res.data.data.communityList[0].id,
                              phone:res.data.data.userInfo.phone=='null'?'':res.data.data.userInfo.phone,
                              addr:res.data.data.userInfo.addr=='null'?'':res.data.data.userInfo.addr,
                              cars:res.data.data.userInfo.cars=='null'?'':res.data.data.userInfo.cars,
                              parkingSpace:res.data.data.userInfo.parkingSpace=='null'?'':res.data.data.userInfo.parkingSpace,
                              number:res.data.data.userInfo.number=='null'?'':res.data.data.userInfo.number
                        })

                        if(this.data.housingId==''){
                              this.setData({
                                    housingId:res.data.data.communityList[0].id
                              })
                        }
                  },
                  complete:()=>{
                        wx.hideLoading();
                  }
            })
      },

      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {

      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            
      },

      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function () {

      },

      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload: function () {

      },

      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function () {

      },

      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function () {

      },

      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function () {

      }
})